<?php

namespace Beerus\Modules\Extend\Controllers;

use App\Http\Controllers\Controller;
use Biz\service\wiki\AdmWiki as sWiki;
use Doraemon\model\Category as mCategory;
use Illuminate\Http\Request;


class CategoryController extends Controller
{
    public function select(Request $request)
    {
        $pid = $request->get('pid',0);


        $list = mCategory::getChildrenById($pid);

        $list = array_map(function ($v){
            return [
                'id'=>$v['id'],
                'pid'=>$v['father_id'],
                'deep'=>$v['deep'],
                'name'=>$v['cate_name']
            ];

        },$list);

        return response()->json($list);
    }

    /**
     * 获取分类信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getChildCategory(Request $request)
    {
        $id = $request->input('q', 0);
        $categories = $id > 0 ? sWiki::getChildCategory($id) : [];
        $categories = array_map(function ($category) {
            return [
                "id" => $category["id"],
                "text" => $category["cate_name"]
            ];
        }, $categories);
        return response()->json($categories);
    }

}
